package com.songtech.login.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.io.IOException;

/**
 * 获取微信信息的工具栏
 */
@Configuration
public class AuthUtil implements EnvironmentAware{
    private static Logger logger = Logger.getLogger(AuthUtil.class);

    private RelaxedPropertyResolver propertyResolver;

    public static String appid;//公众号的唯一标识

    public static String appsecret;//公众号的appsecret

    public static String indexUrl;//域名

    public static String templateId;//模板ID

    public static JSONObject doGetJson(StringBuilder url) throws Exception {
        JSONObject object = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(String.valueOf(url));
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        if(httpEntity!=null){
            String tokens = EntityUtils.toString(httpEntity, "utf-8");
            object = new JSONObject(tokens);
        }
        httpGet.releaseConnection();
        return object;
    }

    public static void doPostJson(String url,String params) throws Exception{
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-Type", "application/json");
        String charSet = "UTF-8";
        StringEntity entity = new StringEntity(params, charSet);
        httpPost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            String tokens = EntityUtils.toString(httpEntity, "utf-8");
            JSONObject object = new JSONObject(tokens);
            if(object != null)logger.info("返回的json数据:"+object.toString());
            StatusLine status = response.getStatusLine();
            int state = status.getStatusCode();
            if (state == HttpStatus.SC_OK) {
                logger.info("消息模板推送成功");
            }
            else{
                logger.error("消息模板推送失败，错误编码:"+state+"("+url+")");
            }
        }
        finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment,"wechat.");
        appid = propertyResolver.getProperty("appid");
        appsecret=propertyResolver.getProperty("appsecret");
        indexUrl=propertyResolver.getProperty("indexUrl");
        templateId=propertyResolver.getProperty("templateId");
    }
}
